package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwbgT;
import cn.e4j.modular.crm.entity.GwbglT;
import cn.e4j.modular.crm.service.GwbgTService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商机主题维护 控制层
 * @author mengyb
 * @date 2019-04-10
 */
@Controller
@RequestMapping("/base/gwat152")
public class Gwat152Controller extends BaseController {

    @Autowired
    private GwbgTService gwbgTService;

    /**
     * 跳转至商机主题维护主界面
     * @return
     */
    @RequiresPermissions("base:gwat152:init")
    @RequestMapping(value = {"","/init"})
    public String index() {
        return "/pc/crm/base/gwat152.html";
    }


    @RequiresPermissions("base:gwat152:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWrapper){
        Dto inDto = dtoWrapper.getDto();
        Page<Object> page=dtoWrapper.getPage();
        List<Map<String, Object>> list = gwbgTService.getList(inDto, page);
        return new PageReData<>(page, list);

    }

    /**
     * 客户开窗（根据权限）
     * @param dtoWrapper
     * @return
     */
    @RequiresPermissions("base:gwat152:init")
    @RequestMapping("/getGwakPageList")
    @ResponseBody
    public ResponseData getGwakPageList(@DtoParam DtoWapper dtoWrapper){
        Dto inDto = dtoWrapper.getDto();
        Page<Object> page=dtoWrapper.getPage();
        List<Map<String, Object>> list = gwbgTService.getGwakPageList(inDto, page);
        return new PageReData<>(page, list);
    }

    /**
     * 获取客户列表（根据权限）
     * @param dtoWrapper
     * @return
     */
    @RequiresPermissions("base:gwat152:init")
    @RequestMapping("/getGwakList")
    @ResponseBody
    public Dto getGwakList(@DtoParam DtoWapper dtoWrapper){
        Dto inDto = dtoWrapper.getDto();

        List<Map<String, Object>> list = gwbgTService.getGwakList(inDto);

        Dto outDto = Dtos.newDto();

        outDto.put("list", list);

        return outDto;
    }

    /**
     * 获取业务员列表
     * @param dtoWrapper
     * @return
     */
    @RequiresPermissions("base:gwat152:init")
    @RequestMapping("/getUserList")
    @ResponseBody
    public Dto getUserList(@DtoParam DtoWapper dtoWrapper){
        Dto inDto = dtoWrapper.getDto();

        List<Map<String, Object>> list = gwbgTService.getUserList(inDto);

        Dto outDto = Dtos.newDto();

        outDto.put("list", list);

        return outDto;
    }


    @RequiresPermissions("base:gwat152:init")
    @RequestMapping("/getGwbg005")
    @ResponseBody
    public ResponseData getGwbg005(){
        return new SuccessResponseData(gwbgTService.getGwbg005());
    }


    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(GwbgT gwbgT, GwbglT gwbglT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbgT.setGWBGENT(shiroUser.getDBC());
        if (gwbgTService.existedTheParams(gwbgT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwbgTService.add(gwbgT, gwbglT);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(GwbgT gwbcT, GwbglT gwbclT) {
        gwbgTService.edit(gwbcT, gwbclT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/delRow")
    @ResponseBody
    public ResponseData delRow(GwbgT gwbgT, GwbglT gwbglT) {
        gwbgTService.delRow(gwbgT, gwbglT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/delRows")
    @ResponseBody
    public ResponseData delRows(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbgT> gwbcTList = inDto.getReqList(GwbgT.class);
        gwbgTService.delRows(gwbcTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/updateGwbg005")
    @ResponseBody
    public ResponseData updateGwbg005(@DtoParam DtoWapper dtoWapper) {
        Dto inDto=dtoWapper.getDto();
        JSONObject jsonObject=inDto.getReqJsonObject();

        List<GwbgT> gwbgTList = JSONObject.parseArray(jsonObject.getString("list"), GwbgT.class);

        gwbgTService.updateGwbg005(gwbgTList,jsonObject.getString("newGwbg005"));

        return SUCCESS_TIP;
    }

}
